
const express = require('express');

const app = express();

app.get('/server', (request, response)=>{

    response.setHeader('Access-Control-Allow-Origin', '*')

    response.send('Hello AJAX Get');
})

app.post('/server', (request, response)=>{

    response.setHeader('Access-Control-Allow-Origin', '*')
    response.send('Hello AJAX POST');
})

app.all('/header', (request, response)=>{

    response.setHeader('Access-Control-Allow-Origin', '*')
    response.setHeader('Access-Control-Allow-Headers', '*')
    response.send('Hello AJAX POST');
})

app.all('/json', (request, response)=>{

    response.setHeader('Access-Control-Allow-Origin', '*')
    response.setHeader('Access-Control-Allow-Headers', '*')

    const data = {
        name:'Kyle',
        age: 18, 
        work: 'programer'
    }
    response.send(JSON.stringify(data));
})

app.all('/delay', (request, response)=>{

    response.setHeader('Access-Control-Allow-Origin', '*')
    response.setHeader('Access-Control-Allow-Headers', '*')
    setTimeout(() => {
        response.send('Hello AJAX Timeout');  
    }, 3000);
})

app.all('/axios', (request, response)=>{

    response.setHeader('Access-Control-Allow-Origin', '*')
    response.setHeader('Access-Control-Allow-Headers', '*')

    const data = {
        name:'Kyle',
        age: 18, 
        work: 'AXIOS'
    }
    response.send(JSON.stringify(data));
})
app.listen(8080, ()=>{
    console.log('服务已启动, 8080端口监听中...');
})

